# !/usr/bin/python3
# -*- coding: utf-8 -*-

# 虎扑娱乐圈
import requests
from lxml import etree


def write_item_to_file(item):
    print("开始写入数据======>>>>>" + str(item))
    with open('html/hupu_casual.html', 'wb') as f:
        f.write(item.encode())
        f.close()


class HupuCasualSpider:

    def __init__(self):
        self.url = "http://nba.hupu.com/"
        # 定义请求头
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
        }

    def get_content(self, url):
        print(url)
        response = requests.get(url, headers=self.headers)
        # print(response.content)
        if response.status_code == 200:
            return response.text
        else:
            return "null"

    def run(self):
        content = self.get_content(self.url)
        # print(content)
        # html = str(content, 'utf-8')
        # print(html)
        write_item_to_file(content)

        # element = etree.parse("html/hupu_casual.html", etree.HTMLParser())
        # print(type(element), element, sep='|')
        #
        # bbs_sl_web_post = element.xpath('//div[@class="bbs-sl-web-post"]/ul/li/div/div/a/text()')
        # print(bbs_sl_web_post)


if __name__ == '__main__':
    spider = HupuCasualSpider()
    spider.run()
